home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu478.dms / pu478.adf / WBGenie / Source / sup_lib.c < prev    next >
C/C++ Source or Header  |  1993-09-12  |  5KB  |  206 lines

  1. /*
  2. **  WBGenie is Copyright 1992 by Steven Velletri.  All Rights Reserved
  3. **
  4. **    Filename:   sup_lib.c
  5. **    Release:    1.0
  6. **    Revision:   0.0
  7. **    Date:       22/05/92
  8. **  Author:     Steven Velletri
  9. **  History:
  10. **
  11. **  Version Author  Date        Reason
  12. **  0.00    SV      23/07/92    Created
  13. **  1.00    SV      08/11/92    Created this version
  14. **
  15. **  To compile this module I used the SAS/C compiler version 5.10a.
  16. */
  17.  
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21.  
  22. #include <exec/types.h>
  23.  
  24. #include "sup_lib.h"
  25.  
  26. char ** sup_get_buf(WORD height, WORD width)
  27.     {
  28.     register WORD i;
  29.     char **buf = calloc(1, height*(sizeof(char *) + width));
  30.  
  31.     if (buf == NULL)
  32.         return(buf);
  33.  
  34.     for (i = 0; i < height; i++)
  35.         {
  36.         *(buf + i) = (char*) buf + (width * i) + (height*sizeof(char *));
  37.         }
  38.     return(buf);
  39.     }
  40.  
  41. BOOL sup_string_scan(char * token, char * str)
  42.     {
  43.  
  44.     /*
  45.     char *tok = token, *s
  46.     */
  47.     BOOL retcd;
  48.  
  49.     retcd = !strnicmp(token, str, strlen(token));
  50.     /*
  51.  
  52.     while(*s++)
  53.         {
  54.         if (*s == *tok)
  55.             {
  56.             while(*tok++)
  57.                 {
  58.                 if *token = *str
  59.  
  60.  
  61.                 }
  62.             }
  63.         }
  64.     */
  65.  
  66.     return(retcd);
  67.     }
  68.  
  69. int sup_get_num_tool_types(char **tool_types)
  70.     {
  71.     int ret_cnt = 0;
  72.  
  73.     while(*(tool_types++))
  74.         {
  75.         ++ret_cnt;
  76.         }
  77.     return(ret_cnt);
  78.     }
  79.  
  80. void sup_free_tool_type_array(char **tool_types)
  81.     {
  82.     while(*(tool_types))
  83.         {
  84.         free(*(tool_types++));
  85.         }
  86.  
  87.     free(tool_types);
  88.     }
  89.  
  90. /******************************************************************************
  91.     sup_create_tool_types()
  92.         This is a support function and should be in a separate module.
  93.         It allocates memory for the new tool type array and inserts
  94.         the passed tool types in this array intelligently.
  95.  
  96. *******************************************************************************/
  97.  
  98. char ** sup_create_tool_type_array(char **old_tool_types,
  99.                                    char **new_tool_types,
  100.                                    struct SUPTOOLTYPES *def_tool_types, int num_new_tt)
  101.     {
  102.  
  103.     register int found, num_tt, i, j;
  104.     char **retcd,
  105.          **ret_tt,
  106.          **new_tt = new_tool_types,
  107.          **old_tt = old_tool_types;
  108.  
  109.     BOOL err = FALSE;
  110.  
  111.     struct SUPTOOLTYPES *def_tt = def_tool_types;
  112.  
  113.     num_tt = sup_get_num_tool_types(old_tool_types);
  114.  
  115.     /* Get memory for new tool types array */
  116.     retcd  = calloc(num_tt + num_new_tt + 1, sizeof(char *));
  117.  
  118.     if (retcd == NULL)
  119.         return(retcd);
  120.  
  121.     /* Ensure that flags are initialised to FALSE */
  122.     for(def_tt = def_tool_types, i = 0; i < num_new_tt; i++, def_tt++)
  123.         {
  124.         def_tt->flag1 = FALSE;
  125.         }
  126.  
  127.     /*
  128.     ** This double loop finds all the pre-existing tools types, changes them
  129.     ** to the required value and marks the tool type as being processed
  130.     */
  131.  
  132.     for (ret_tt = retcd; *old_tt; old_tt++, ret_tt++)
  133.         {
  134.         for (i = 0, def_tt = def_tool_types; def_tt->tt_name, i < num_new_tt; def_tt++, i++)
  135.             {
  136.             found = FALSE;
  137.  
  138.             if (sup_string_scan(def_tt->tt_name, *old_tt))
  139.                 {
  140.                 for(new_tt = new_tool_types, j = 0; j < num_new_tt; j++, new_tt++)
  141.                     {
  142.                     if ((sup_string_scan(def_tt->tt_name, *new_tt)))
  143.                         {
  144.                         if (def_tt->flag1)
  145.                             break;
  146.                         *ret_tt = strdup(*new_tt);
  147.  
  148.                         def_tt->flag1 = found = TRUE;
  149.                         break;
  150.                         }
  151.                     }
  152.                 break;
  153.                 }
  154.             }
  155.         if (!found)
  156.             *ret_tt = strdup(*old_tt);
  157.  
  158.         if (*ret_tt == NULL)
  159.             {
  160.             err = TRUE;
  161.             break;
  162.             }
  163.         }
  164.  
  165.     if (err) /* an error occured */
  166.         {
  167.         sup_free_tool_type_array(ret_tt);
  168.         return(NULL);
  169.         }
  170.  
  171.     /*
  172.     ** This double loop adds the tools which were not marked as found
  173.     */
  174.  
  175.     for(new_tt = new_tool_types, i = 0; i < num_new_tt; i++, new_tt++)
  176.         {
  177.         found = FALSE;
  178.  
  179.         for(def_tt = def_tool_types, j = 0; j < num_new_tt; j++, def_tt++)
  180.             {
  181.             if (!def_tt->flag1 && sup_string_scan(def_tt->tt_name, *new_tt))
  182.                 {
  183.                 found = TRUE;
  184.                 *ret_tt = strdup(*new_tt);
  185.                 break;
  186.                 }
  187.             }
  188.         if (found && *ret_tt++ == NULL)
  189.             {
  190.             err = TRUE;
  191.             break;
  192.             }
  193.         }
  194.  
  195.     if (err) /* an error occured */
  196.         {
  197.         sup_free_tool_type_array(ret_tt);
  198.         return(NULL);
  199.         }
  200.     *(ret_tt) = NULL;
  201.  
  202.     /* No errors */
  203.  
  204.     return(retcd);
  205.     }
  206.